<template>
  <div>
    <el-card>
      <el-button type="primary" @click="goBack">返回</el-button>
      <el-button style="margin-bottom:20px" @click="showAdd"
        >添加监控项</el-button
      >
      <el-table :data="monitorList" border style="width: 100%">
        <el-table-column
          prop="id"
          label="监控项Id  "
          width="180"
        ></el-table-column>
        <el-table-column
          prop="name"
          label="监控项名称"
          width="180"
        ></el-table-column>
        <el-table-column prop="monitorType" label="参数类型" width="180">
          <template slot-scope="scoped">
            <div>
              <el-tag v-if="scoped.row.monitorType == 1">电压</el-tag>
              <el-tag type="success" v-else-if="scoped.row.monitorType == 2"
                >电流</el-tag
              >
              <el-tag type="info" v-else-if="scoped.row.monitorType == 3"
                >温度</el-tag
              >
              <el-tag type="warning" v-else-if="scoped.row.monitorType == 4"
                >总公率</el-tag
              >
              <el-tag type="danger" v-else-if="scoped.row.monitorType == 5"
                >各路功率</el-tag
              >
              <el-tag type="info" v-else-if="scoped.row.monitorType == 6"
                >环境温度</el-tag
              >
              <el-tag type="success" v-else-if="scoped.row.monitorType == 7"
                >环境湿度</el-tag
              >
            </div>
          </template>
        </el-table-column>
        <el-table-column
          prop="passNum"
          label="通道"
          width="180"
        ></el-table-column>
        <el-table-column
          prop="add_time"
          label="添加时间"
          width="180"
        ></el-table-column>
        <el-table-column prop="unit" label="参数单位"></el-table-column>
        <el-table-column fixed="right" label="操作" width="200">
          <template slot-scope="scoped">
            <el-button type="success" size="small" @click="showEdit(scoped.row)"
              >编辑</el-button
            >
            <el-button
              type="danger"
              size="small"
              @click="handlerDel(scoped.row.id)"
              >删除</el-button
            >
          </template>
        </el-table-column>
      </el-table>
      <div class="pagination" style="margin-bottom:20px;">
        <el-pagination
          background
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          :page-sizes="[10, 20, 30, 40, 50]"
          :page-size="monitorQuery.pageSize"
          :current-page="monitorQuery.pageNumber"
          layout="total, sizes, prev, pager, next, jumper"
          :total="count"
          class="management_page"
        ></el-pagination>
      </div>
      <!-- 监控项弹框 -->
      <el-dialog
        :title="monitorName"
        :visible.sync="dialogVisible"
        width="30%"
        :before-close="handleClose"
      >
        <el-form
          :model="ruleForm"
          :rules="rules"
          ref="ruleFormRef"
          label-width="100px"
          class="demo-ruleForm"
        >
          <!-- <el-form-item label="公司名称(必填):" prop="companyId">
            <el-select
              v-model="ruleForm.companyId"
              clearable
              placeholder="请选择公司名称"
              @change="handlercompanyId"
            >
              <el-option
                v-for="item in companySelList"
                :label="item.name"
                :value="item.id"
                :key="item.id"
              ></el-option>
            </el-select>
          </el-form-item>-->
          <el-form-item label="监控项名称" prop="name">
            <el-input
              v-model="ruleForm.name"
              placeholder="请输入监控项名称"
            ></el-input>
          </el-form-item>
          <el-form-item label="参数类型" prop="monitorType">
            <el-select
              v-model="ruleForm.monitorType"
              placeholder="请选择参数类型"
              @change="handlergetDanwei"
            >
              <el-option
                v-for="item in monitorTypeList"
                :label="item.monitorName"
                :value="item.monitorType"
                :key="item.monitorType"
              ></el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="通道" prop="passNum">
            <el-input
              v-model="ruleForm.passNum"
              placeholder="请输入通道"
            ></el-input>
          </el-form-item>
          <el-form-item label="参数单位" prop="unit">
            <el-input
              readonly
              v-model="ruleForm.unit"
              placeholder="单位"
            ></el-input>
          </el-form-item>
        </el-form>
        <span slot="footer" class="dialog-footer">
          <el-button @click="handlerQu">取 消</el-button>
          <el-button type="primary" @click="handlerSure">确 定</el-button>
        </span>
      </el-dialog>
    </el-card>
  </div>
</template>

<script>
import {
  monitorList,
  monitorAdd1,
  monitorUpdate,
  monitorDel,
  companySel,
  getMonitorInfo
} from "../../utils/api";
import { mapState, mapGetters } from "vuex";
export default {
  data() {
    return {
      // 监控列表
      monitorList: [],
      //   监控管理分页
      monitorQuery: {
        pageNumber: 1,
        pageSize: 10,
        companyId: ""
      },
      //总条数
      count: 0,
      //   弹框标题
      monitorName: "添加监控项",
      // 监控项显示隐藏
      dialogVisible: false,
      //   监控数据
      ruleForm: {
        name: "",
        monitorType: "",
        passNum: "",
        unit: "",
        userId: "",
        companyId: "",
        id: ""
      },
      //   监控项校验
      rules: {
        name: [
          { required: true, message: "请输入监控项名称", trigger: "blur" }
        ],
        monitorType: [
          { required: true, message: "请选择参数类型", trigger: "blur" }
        ],
        passNum: [{ required: true, message: "请输入通道", trigger: "blur" }],
        unit: [{ required: true, message: "请输入参数单位", trigger: "blur" }]
      },
      //   参数数据
      monitorTypeList: [],
      // 公司的列表
      companySelList: []
    };
  },
  computed: {
    ...mapState(["org_id"])
  },
  watch: {
    org_id(val) {
      this.getmonitorList();
    }
  },
  created() {
    this.getmonitorList();
  },
  methods: {
    // 获取参数类型
    handlergetMonitorInfo() {
      this.postRequest(getMonitorInfo).then(res => {
        if ((res.code = 200)) {
          this.monitorTypeList = res.data;
        }
      });
    },
    // 获取温控参数对应的单位
    handlergetDanwei(val) {
      this.monitorTypeList.map(item => {
        if (val == item.monitorType) {
          this.ruleForm.unit = item.monitorUnit;
        }
      });
    },
    // 返回
    goBack() {
      this.$store.dispatch("delVisitedViews", this.$route).then(obj => {
        this.$router.push("/MonitoringControl");
      });
    },
    //   获取监控列表
    getmonitorList() {
      this.monitorQuery.companyId = this.org_id;
      this.postRequest(monitorList, this.monitorQuery).then(res => {
        if (res.code == 200) {
          this.monitorList = res.data.data;
          this.count = res.data.count;
        }
      });
    },
    // 分页
    handleSizeChange(val) {
      this.monitorQuery.pageSize = val;
      this.getmonitorList();
    },
    handleCurrentChange(val) {
      this.monitorQuery.pageNumber = val;
      this.getmonitorList();
    },
    //点击空白触发的函数
    handleClose() {
      this.dialogVisible = false;
      this.$refs.ruleFormRef.resetFields();
    },
    // 显示添加的监控项弹框
    showAdd() {
      this.monitorName = "添加监控项";
      this.handlergetMonitorInfo();
      this.dialogVisible = !this.dialogVisible;
    },
    // 点击取消
    handlerQu() {
      this.dialogVisible = false;
      this.$refs.ruleFormRef.resetFields();
    },
    // 点击编辑的函数
    showEdit(data) {
      this.monitorName = "编辑监控项";
      this.dialogVisible = !this.dialogVisible;
      this.handlergetMonitorInfo();
      // console.log(data);
      this.$nextTick(() => {
        this.ruleForm.name = data.name;
        this.ruleForm.monitorType = data.monitorType;
        this.ruleForm.passNum = data.passNum;
        this.ruleForm.unit = data.unit;
        this.ruleForm.id = data.id;
      });
    },
    // 封装一个添加监控项的方法
    handlerAdd() {
      this.ruleForm.companyId = this.org_id;
      this.ruleForm.userId = window.sessionStorage.getItem("userId");
      this.postRequest(monitorAdd1, this.ruleForm).then(res => {
        if (res.code == 200) {
          this.$message.success("添加监控项成功");
          this.getmonitorList();
          this.dialogVisible = false;
          this.$refs.ruleFormRef.resetFields();
        }
      });
    },
    // 封装一个编辑监控项的方法
    handlerEdit() {
      this.ruleForm.companyId = this.org_id;
      this.ruleForm.userId = window.sessionStorage.getItem("userId");
      this.postRequest(monitorUpdate, this.ruleForm).then(res => {
        if (res.code == 200) {
          this.$message.success("编辑监控项成功");
          this.getmonitorList();
          this.dialogVisible = false;
          this.$refs.ruleFormRef.resetFields();
        }
      });
    },
    // 点击确定 判断是添加监控项 还是修改监控项
    handlerSure() {
      this.$refs.ruleFormRef.validate(valid => {
        if (!valid) return this.$message.error("请填写完整");
        if (this.monitorName == "添加监控项") {
          this.handlerAdd();
        } else if (this.monitorName == "编辑监控项") {
          this.handlerEdit();
        }
      });
    },
    // 点击删除
    handlerDel(id) {
      const params = {
        id: id,
        userId: window.sessionStorage.getItem("userId")
      };
      this.$confirm("此操作将永久删除该文件, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      })
        .then(() => {
          this.postRequest(monitorDel, params).then(res => {
            if (res.code == 200) {
              this.$message({
                type: "success",
                message: "删除成功!"
              });
              this.getmonitorList();
            }
          });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除"
          });
        });
    }
    //  获取公司的无分页列表
    // getcompanySel() {
    //   this.postRequest(companySel).then(res => {
    //     if (res.code == 200) {
    //       this.companySelList = res.data;
    //     }
    //   });
    // },
    //  拿到公司的id
    // handlercompanyId(val) {
    //   this.ruleForm.companyId = val;
    // }
  }
};
</script>

<style></style>
